home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
ASSEMBLE
/
MDSCVT.TXT
< prev
Wrap
Text File
|
1990-08-25
|
6KB
|
191 lines
Converting Assembly Language Files from MDS to MPW Format
August 20, 1990
Jon Bell
Dept. of Physics & Computer Science
Presbyterian College
Clinton SC 29325
(CIS #70441,353)
(inspired by an article by Dan Weston,
in the June 1989 issue of MacTutor magazine)
Introduction
------------
Most Macintosh assembly-language programming now seems to be done with
the Macintosh Programmer's Workshop (MPW) assembler from Apple. Unfor-
tunately, most books on Macintosh assembly language programming were
written for the MPW assembler's predecessor, the Macintosh Development
System (MDS), originally sold by Apple but now distributed by Consulair.
This note describes the "bare minimum" changes which need to be made
to a program to convert it from MDS to MPW format. These should be
sufficient to convert the example programs in Dan Weston's "Complete Book
of Macintosh Assembly Language Programming."
1. Declaring Procedures
------------------------
The basic unit of code in MPW is the "code module". Your source program
must contain at least one code module. A module begins with one of the
following directives:
MAIN for a main program (must have exactly one)
PROC for a procedure
FUNC for a function
A module can end either with ENDMAIN, ENDPROC or ENDFUNC, or with the
MAIN, PROC or FUNC which begins the next code module.
I usually put the program, procedure or function name in the MAIN, PROC
or FUNC directive:
MyProgram MAIN
...
ENDMAIN
2. Separating code and data
----------------------------
Assembly language programs use DS directives to declare global variables.
In MDS, you can place the DS directives wherever you like in your program;
the linker collects all the globals together and allocates one block
of memory for them. In MPW you can do much the same thing. However, it
makes a difference whether you put the DS directives inside or outside
a code module. If you put DS directives _inside_ a code module, you must
set off each group of DS directives by placing a DATA directive before
it and a CODE directive after it:
MyProgram MAIN
DATA
var1 DS.B 1
var2 DS.L 1
... ...
CODE
(instructions)
DATA
(more variables)
CODE
(more instructions)
ENDMAIN
If you put all your DS directives _outside_ the code module, either before it
or after it, you don't have to worry about this.
3. Declaring the QuickDraw globals
-----------------------------------
Practically all MDS programs have the following "magic incantation" at
the beginning of the initialization section:
pea -4(A5)
_InitGraf
The parameter to InitGraf is actually tells InitGraf where to put the
block of global variables which QuickDraw uses. In particular, the
parameter is a pointer to the variable "thePort", which (after initialization)
becomes a pointer to the current GrafPort. "ThePort" is located at the top of
the block of QuickDraw globals, which is 106 bytes long. The first byte of
"thePort" is the 103rd byte of the QD globals, because we count the bytes
upward from the bottom of the block.
The MDS linker, by default, reserves space for the QD globals
immediately below the address stored in register A5 (the "A5 boundary").
It does this by allocating your own globals beginning 256 bytes below
the A5 boundary. Since the QD globals occupy only 106 bytes,
this actually leaves some unused space between the QD globals and your
own globals.
The MPW linker, on the other hand, does _not_ automatically allocate
space for the QuickDraw globals, but simply begins allocating _your_
globals immediately below the A5 boundary. If you blindly use "pea -4(A5)" to
set up InitGraf, the QuickDraw globals will end up occupying space which
was reserved for _your_ globals. When you put a value in the global
variable which occupies the space where QuickDraw _thinks_ "thePort" is,
QuickDraw will take that value as the address of the current GrafPort,
and *** blammo! ***
Therefore, in MPW, _you_ must explicitly declare the QuickDraw globals
in your program, and pass InitGraf a pointer to "thePort". The simplest
way to do this is to declare
QDGlobals DS.B GrafSize
along with your other global variables, then replace the "magic incantation"
with
pea QDGlobals+GrafSize-4(A5)
_InitGraf
The constant "GrafSize" is defined as 106 (the total size of the QD globals
area) in the include file "QuickEqu.D". The effective address therefore
points 4 bytes into the QD globals, starting from the top, which is the
location of "thePort". Of course, you could also write this as
pea QDGlobals+102(A5)
_InitGraf
but I like to use the predefined constants wherever possible.
Note that you can do exactly the same thing in MDS. In this case you
can tell the linker to start your globals immediately below the A5
boundary by including the command "/Globals 0" in your link control file.
4. Changing Directives
-----------------------
If your MDS program is divided into more than one source file, it probably
uses XDEF and XREF directives to allow one file to use symbols defined
in another one. MPW uses the directives EXPORT and IMPORT in their place.
Simply replace XDEF and XREF with EXPORT and IMPORT wherever they occur.
There may be other directives that need changing, but I haven't found any
yet, at least not in Dan Weston's example programs.
5. Labels
----------
In MDS, labels customarily begin in the first column of a line, but you
can indent them if you follow them with a colon:
MyRecord DS.L 0
Field1: DS.L 1
Field2: DS.L 1
Field3: DS.W 1
This comes in handy sometimes for defining record-like structures.
In MPW, you cannot do this. All labels _must_ start in the first column.
6. Macros
----------
MPW macros have a different format from MDS macros:
MDS: MACRO _SFGetFile =
move.w #2, -(SP)
_Pack3
|
MPW: MACRO
_SFGetFile
move.w #2, -(SP)
_Pack3
ENDM
If you run into any problems with the above guidelines, or if you encounter
any other things which need to be converted, please feel free to drop me a
line at the address above, or post me a message in MACDEV, or send me an e-mail.